using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Net.Mime;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content.Pipeline;
using Microsoft.Xna.Framework.Content.Pipeline.Graphics;
using XnaColor = Microsoft.Xna.Framework.Graphics.Color;
using DrawingColor = System.Drawing.Color;

using TImport = Microsoft.Xna.Framework.Content.Pipeline.Graphics.TextureContent;

namespace ContentExtentions
{
    /// <summary>
    /// This class will be instantiated by the XNA Framework Content Pipeline
    /// to import a file from disk into the specified type, TImport.
    /// 
    /// This should be part of a Content Pipeline Extension Library project.
    /// </summary>
    [ContentImporter(".gif", DisplayName = "Gif Importer", DefaultProcessor = "None")]
    public class GifImporter : ContentImporter<TImport>
    {
        public override TImport Import(string filename, ContentImporterContext context)
        {
            Bitmap bitmap = (Bitmap)Image.FromFile( filename );

            PixelBitmapContent<XnaColor> pixelBitmapContent =
                    new PixelBitmapContent<XnaColor>( bitmap.Width, bitmap.Height );

            for (int y = 0; y < bitmap.Height; y++)
            {
                for (int x = 0; x < bitmap.Width; x++)
                {
                    DrawingColor color = bitmap.GetPixel( x, y );
                    pixelBitmapContent.SetPixel( x, y, new XnaColor( color.R, color.G, color.B, color.A ) );
                }
            }

            Texture2DContent texture2DContent = new Texture2DContent();
            texture2DContent.Mipmaps.Add( pixelBitmapContent );

            return texture2DContent;
        }
    }
}